﻿using Common.Contract;
using Common.Contract.Customer.Identifier;
using Common.Contract.Customer.Record;
using Common.Contract.DataCenter.Record;
using Common.Contract.DataCenter.Request;
using Common.Contract.SaleOut.Record;
using Common.Service.Command;
using Common.Service.Queue;
using Common.Util;
using DataCenter.Configuration;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataCenter.ServiceCommand
{
    public class UpdateCdcHistoryServiceCommand : ProviderQueueCommand<IProviderFactory, CdcHistRecord, ResponseRecord>
    {
        public UpdateCdcHistoryServiceCommand(IProviderFactory provider) : base(provider) { }
        protected override void Validate(CdcHistRecord record)
        {
            if (record == null) throw new ArgumentNullException("request");
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var cdcHist = Record;
            var obj = new SerialNumberCdcHist()
            {
                BoxNo = cdcHist.BoxNo,
                ClosedDate = cdcHist.ClosedDate,
                CustomerCode = cdcHist.CustomerCode,
                CustomerName = cdcHist.CustomerName,
                IncentiveFlag = cdcHist.IncentiveFlag.HasValue ? cdcHist.IncentiveFlag.Value.ToBooleanString() : null,
                InvoiceDate = cdcHist.InvoiceDate,
                InvoiceNo = cdcHist.InvoiceNumber,
                InvOrg = cdcHist.InvOrg,
                LoadplanNo = cdcHist.LoadplanNo,
                Model = cdcHist.Model,
                ModelSuffix = cdcHist.ModelSuffix,
                OrderNo = cdcHist.OrderNumber,
                SelloutDate = cdcHist.SelloutDate,
                SelloutFlag = cdcHist.SelloutFlag.HasValue ? cdcHist.SelloutFlag.Value.ToBooleanString() : null,
                SeqNo = cdcHist.SeqNumber,
                SerialNo = cdcHist.SerialNo,
                ShiptoCode = cdcHist.ShipCode,
                ShiptoDate = cdcHist.ShipDate,
                ShiptoName = cdcHist.ShipName,
                SoTransferDate = DateTime.Now,
                SoTransferFlag = BooleanString.TRUE,
                Suffix = cdcHist.Suffix,
                UniqueId = cdcHist.UniqueId,
                WarrantyIfDate = cdcHist.WarrantyInfoDate,
                WarrantyIfFlag = cdcHist.WarrantyInfoFlag.HasValue ? cdcHist.WarrantyInfoFlag.Value.ToBooleanString() : null,
            };
            var itemCheck = Providers.SerialNumberCdcHist.Retrieve(Record.Model, Record.Suffix, Record.SerialNo);
            if (itemCheck == null)
            {
                var resultitem1 = Providers.SerialNumberCdcHist.Add(obj);
            }
            else
            {
                var resultitem2 = Providers.SerialNumberCdcHist.Update(obj);
            }
            return true;
        }
    }
}
